<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Settlement extends Admin_Controller {

	function __construct ()
	{
		parent::__construct();
        $this->load->model('project_m');
        $this->load->model('userinto_m');
        $this->load->model('settlement_m');
        $this->load->library('pagination');
		if(!$this->auth->is_admins()){
			redirect(site_url('user/login'));
		}
	}
	
	public function index($page=1)
	{
	    $key=!empty($this->input->get('user_uname'))?$this->input->get('user_uname'):'';
        $limit = 30;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['reuse_query_string'] = TRUE;
        $config['base_url'] = base_url('manage/settlement/index/');
        $config['total_rows'] = $this->settlement_m->count_settlement_find($key);
        $config['per_page'] = $limit;
        $config['num_links'] = 10;
        $start = ($page-1)*$limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links($key);
        $user_id = $this->session->userdata('user_id');
        $user = $this->user_m->get_user_by_user_id($user_id);
        if($user['user_group']==4){
            $data['settlements'] = $this->settlement_m->get_user_settlement_list($start,$limit,$key,$user_id);
        }else{
            $data['settlements'] = $this->settlement_m->get_settlement_list($start,$limit,$key);
        }

        $data['key']=$key;
		$this->load->view('settlement/index',$data);
	}
	//新增成员
    public function add($project_id='',$user_id=''){
	    if($project_id && $user_id){
            $data['projects'] = $this->project_m->get_all_project();
            $data['users'] = $this->user_m->get_users();
            $settlement['user_id']=$user_id;
            $settlement['project_id']=$project_id;
            $data['settlement']=$settlement;
        }else{
            $data['projects'] = $this->project_m->get_all_project();
            $data['users'] = $this->user_m->get_users();
        }
	    $this->load->view('settlement/form',$data);
    }
    //更新信息
    public function update(){
        $post = $this->input->post();
        $settlement_id = isset($post['settlement_id']) ? intval($post['settlement_id']) : false;
        $userinto=$this->userinto_m->get_project($post['user_id'],$post['project_id']);
        $project = $this->project_m->get_one_project($post['project_id']);
        $settlementmomey=$this->settlement_m->count_settlement_monney($post['user_id'],$post['project_id']);
        if($settlementmomey+$post['settlement_money']>$userinto['into_num']*$project['project_money']){
            show_message('添加失败,添加金额不能大于总金额',site_url(X_URL.'settlement/index'),1);
        }

        if (!$settlement_id) {
            $settlement = array(
                'user_id' => $post['user_id'],
                'project_id' =>$post['project_id'],
                'settlement_money' =>  $post['settlement_money'],
                'settlement_time'=> time(),
            );
            $result = $this->settlement_m->add_settlement($settlement);
            if($result){
                show_message('新增成功',site_url(X_URL.'settlement/index'),1);
            }else{

            }
        } else {
            // 修改
            $settlement = array(
                'user_id' => $post['user_id'],
                'project_id' =>$post['project_id'],
                'settlement_money' =>  $post['settlement_money'],
            );
            $result =$this->settlement_m->update_settlement($settlement_id,$settlement);
            if($result){
                show_message('修改成功',site_url(X_URL.'settlement/index'),1);
            }else{
                show_message('修改失败',site_url(X_URL.'settlement/index'),1);
            }
        }
    }
    public function edit($settlement_id){
        if ($settlement_id) {
            $settlement = $this->settlement_m->get_one_settlement($settlement_id);
        } else {
            show_message('参数错误',site_url(X_URL.'project/index'),1);
        }
        $data['projects'] = $this->project_m->get_all_project();
        $data['users'] = $this->user_m->get_users();
        $data['settlement'] = $settlement;
        $this->load->view('settlement/form',$data);
    }

    public function delete($settlement_id){
        if($settlement_id){
            $result = $this->settlement_m->update_settlement($settlement_id,array('delete'=>1));
            if($result){
                show_message('删除成功',site_url(X_URL.'settlement/index'),1);
            }else{
                show_message('删除失败',site_url(X_URL.'settlement/index'),1);
            }
        }else{
            show_message('参数错误',site_url(X_URL.'settlement/index'),1);
        }
    }


}